<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades" /> 角色管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="add"
        >新增</el-button>
        <el-input v-model="query.name" placeholder="角色名" class="handle-input mr10" />
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        border
        class="table"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="roleName" label="角色名称" />
        <el-table-column prop="roleDescribe" label="角色描述" />
        <el-table-column prop="userStatus" label="角色权限" />
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        />
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="65%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="角色名称">
          <el-input v-model="form.roleName" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="form.roleDescribe" />
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree
            ref="tree"
            :data="options"
            show-checkbox
            default-expand-all
            node-key="menuId"
            highlight-current
            :props="{ checkStrictly: true ,value:'menuId',label:'menuName',children:'menus',emitPath:false,expandTrigger:'hover'}"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getRoles } from '@/api/role'
import { getTree } from '@/api/menu'
import { saveRole } from '@/api/role'
export default {
  name: 'Basetable',
  data() {
    return {
      form: {},
      multipleSelection: [],
      tableData: [],
      pageTotal: 50,
      editVisible: false,
      query: {
        pageNumber: 1,
        pageSize: 10,
        sort: '',
        order: '',
        entity: {

        }
      },
      menus: [],
      options: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 获取 easy-mock 的模拟数据
    getData() {
      getRoles(this.query).then(res => {
        this.$set(this.query, 'pageIndex', 1)
        this.tableData = res.data.list
        this.pageTotal = res.data.total || 50
      })
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, 'pageIndex', 1)
      this.getData()
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    delAllSelection() {
      const length = this.multipleSelection.length
      let str = ''
      this.delList = this.delList.concat(this.multipleSelection)
      for (let i = 0; i < length; i++) {
        str += this.multipleSelection[i].name + ' '
      }
      this.$message.error(`删除了${str}`)
      this.multipleSelection = []
    },
    // 编辑操作
    handleEdit(row) {
      this.form = row
      this.editVisible = true
      const nodes = []
      for (const ent of row.menus) {
        nodes.push(ent.menuId)
      }

      getTree().then(res => {
        console.log(res.data.tree)
        this.options = res.data.tree
        this.$refs.tree.setCheckedKeys(nodes)
      })
    },
    // 保存编辑
    saveEdit() {
      this.editVisible = false
      this.form.menus = this.multipleSelection
      const nodes = this.$refs.tree.getCheckedNodes()
      console.log(nodes)
      this.form.menus = nodes
      saveRole(this.form).then(res => {
        this.$message.success(res.message)
      })
    },
    getCheckedKeys() {
      console.log(this.$refs.tree.getCheckedKeys())
    },
    add() {
      this.form = {}
      this.editVisible = true
      getTree().then(res => {
        this.options = res.data.tree
      })
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val)
      this.getData()
    }
  }
}
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
